package com.zheng.api.server.service.mq.um;

import com.alibaba.fastjson.JSONObject;
import com.zheng.api.common.constant.ApiConstant;
import com.zheng.api.common.constant.ApiResult;
import com.zheng.common.constant.MqConstant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.JsonUtil;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.jms.Topic;
import java.util.Date;

/**
 * @Description: 小程序通知MQ统一处理
 * @author lvb
 * @date 2017-10-31
 * @version V1.0
 * @Copyright:
 */
@Service
public class Mq4UmbrellaWechatappNotify extends Mq4UmbrellaWechatapp {

    private Topic umbrellaWechatappTopic = new ActiveMQTopic(MqConstant.ZHENG_CLIENT_TOPIC_UMBRELLA_WECHATAPP_NAME);

    //private Queue umbrellaWechatappQueue = new ActiveMQQueue(MqConstant.ZHENG_SERVER_QUEUE_UMBRELLA_WECHATAPP_NAME);

//    @Autowired
//   private WechatBusiness4CommandServiceV1 wechatBusiness4CommandService;

    private static final Logger LOGGER = LoggerFactory.getLogger(Mq4UmbrellaWechatappNotify.class);

    public void send(String msg,String clientId) {
        //小程序用户Id增加前缀
        String logPrefix = "【后台向小程序发送指令通知】";
        Date beginTime = DateUtil.getNowDate();
        try {
            String composeCliendId = "userid:" + clientId ;
            super.send(umbrellaWechatappTopic, msg ,20,composeCliendId);
        }catch (Exception e) {
            LOGGER.error("{}发送消息:msg={}", logPrefix, msg,e);
        }finally{
            Date endTime = DateUtil.getNowDate();
            super.recordUmbrellaHardwareMqttcommandLog(beginTime,msg,endTime,null);
        }
    }

    //@JmsListener(destination = MqConstant.ZHENG_SERVER_TOPIC_UMBRELLA_WECHATAPP_NAME,containerFactory="jmsTopicListenerContainer")
    //public void receive(byte[] bytes) {
    public void receive(String msg) {
        String logPrefix = "【后台接收小程序指令通知】";
        //String msg = new String(bytes);
        LOGGER.info("{}接收消息:msg={}", logPrefix, msg);
        ApiResult result = null;
        Date beginTime = DateUtil.getNowDate();
        try {
            JSONObject jsonMsg = JsonUtil.getJSONObjectFromJson(msg);
            String command = JsonUtil.getString(jsonMsg,"command");
            String machineCode = JsonUtil.getString(jsonMsg,"machineCode");
            //机器编号需要特殊处理
             machineCode = machineCode==null?"":machineCode.substring(machineCode.lastIndexOf("/")+1);if("null".equals(machineCode)){machineCode = "";}
            int code = JsonUtil.getInteger(jsonMsg,"code");
            String message = JsonUtil.getString(jsonMsg,"message");
            JSONObject recvData = JsonUtil.getJSONObject(jsonMsg,"data");
            if(StringUtils.isEmpty(command)){
                LOGGER.error("{}接收消息:msg={}", "参数不正确，command为空", msg);
            }else{
                switch (command) {
                    case ApiConstant.UMBRELLA_WECHATAPP_COMMAND_40101:
                        //扫码下借伞订单(http/mqtt)【40101】
                        result = super.baseWechatBusinessCommandFactory(machineCode).doCommand40101(command,machineCode, code, message, recvData,true);
                        break;
                    case ApiConstant.UMBRELLA_WECHATAPP_COMMAND_40201:
                        //扫码预约还伞(http/mqtt)【40201】
                        result = super.baseWechatBusinessCommandFactory(machineCode).doCommand40201(command,machineCode, code, message, recvData,true);
                        break;
                    case ApiConstant.UMBRELLA_WECHATAPP_COMMAND_40601:
                        //雨伞管理-进伞(http/mqtt)【40601】
                        result = super.baseWechatBusinessCommandFactory(machineCode).doCommand40601(command,machineCode, code, message, recvData,true);
                        break;
                    case ApiConstant.UMBRELLA_WECHATAPP_COMMAND_40602:
                        //雨伞管理-结束进伞(http/mqtt)【40602】
                        result = super.baseWechatBusinessCommandFactory(machineCode).doCommand40602(command,machineCode, code, message, recvData,true);
                        break;
                    case ApiConstant.UMBRELLA_WECHATAPP_COMMAND_40701:
                        //雨伞管理-出伞(http/mqtt)【40701】
                        result = super.baseWechatBusinessCommandFactory(machineCode).doCommand40701(command,machineCode, code, message, recvData,true);
                        break;
                    case ApiConstant.UMBRELLA_WECHATAPP_COMMAND_40702:
                        //雨伞管理-结束出伞(http/mqtt)【40702】
                        result = super.baseWechatBusinessCommandFactory(machineCode).doCommand40702(command,machineCode, code, message, recvData,true);
                        break;
                    default:
                        LOGGER.error("{}接收消息:msg={}", logPrefix, msg,"指令不正确");
                        break;
                }
            }
        }catch (Exception e) {
            LOGGER.error("{}接收消息:msg={}", logPrefix, msg,e);
        }finally{
            Date endTime = DateUtil.getNowDate();
            super.recordUmbrellaHardwareMqttcommandLog(beginTime,msg,endTime,JsonUtil.object2Json(result));
        }
        LOGGER.info("{}处理消息完成:msg={}", logPrefix, msg);
    }
}
